# Gauging Preference Stability under Authoritarianism
# Jennifer Pan & Yiqing Xu
# 2025

## Master script to run all the analyses

# install packages
install_all <- function(packages) {
  installed_pkgs <- installed.packages()[, "Package"]
  for (pkg in packages) {
    if (!pkg %in% installed_pkgs) {
      install.packages(pkg)
    }
  }
}
packages <- c("Amelia", "corrplot", "doParallel", "dplyr", "fixest", "foreach", 
              "GGally", "ggplot2", "gridExtra", "haven", "likert", "psy", 
              "psych", "readr", "scales", "tidyr", "writexl")
install_all(packages)

# load all packages
for (pkg in packages) {
  library(pkg, character.only = TRUE)
}

# Run R scripts sequentially

########################
## Supporting code 
########################

## Note: only needed for a full replication
## the resulting files are provided in the "output" folder

# source("code/support/preapre_sample1.R") # run-time with 4 cores: 90 mins
# source("code/support/preapre_sample2.R") # run-time with 4 cores: 60 mins
# source("code/support/get_stability.R") # run-time with 16 cores: 40 mins
# source("code/support/find_set.R") # run-time: 2 hours

########################
## Main code 
########################

### Run sequentially
### Takes <10 mins

source("code/1_dimensionality.R")
source("code/2_measures.R")
source("code/3_stability.R")
source("code/4_plot_stat.R")
source("code/5_college.R")
source("code/6_sm_stats.R")
source("code/7_sm_reg.R")
source("code/8_sm_bestset.R")


